package com.woniu.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.ExpenseDetail;
import org.apache.ibatis.annotations.*;


import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author hyl
 * @since 2022-08-28
 */
public interface ExpenseDetailMapper extends BaseMapper<ExpenseDetail> {
    //全查
    @Select("select * from expense_detail,user,expense,dept where expense_detail.user_id = user.user_id and expense_detail.exp_id = expense.exp_id and expense_detail.dept_id = dept.dept_id")
    @Results(id = "expenseDetailMap",value = {
            @Result(column = "user_id",property = "user.userId"),
            @Result(column = "user_name",property = "user.userName"),
            @Result(column = "exp_id",property = "expense.expId"),
            @Result(column = "exp_category",property = "expense.expCategory"),
            @Result(column = "dept_id",property = "dept.deptId"),
            @Result(column = "dept_name", property = "dept.deptName"),
    })
    List<ExpenseDetail> getAll();

    //根据报销单号查询
    //EDID -->exp_detail_id
    @Select("select * from expense_detail where exp_detail_id = #{edid}")
    List<ExpenseDetail> getByEDId(int edid);

    //根据报销类别号查询
    //EID -->exp_id
    @Select("select * from expense_detail where exp_id = #{eid}")
    List<ExpenseDetail> getByEId(int eid);

    //根据银行账号查询
    //BID -->bank_id
    @Select("select * from expense_detail where bank_id = #{bid}")
    List<ExpenseDetail> getByBId(int bid);

    //根据部门ID查询
    //DID -->dept_id
    @Select("select * from expense_detail where dept_id = #{did}")
    List<ExpenseDetail> getByDId(int did);

    //根据报销时间查询支持模糊查询
    //EDate -->exp_date
    @Select("select * from expense_detail where exp_date like %#{edate}%")
    List<ExpenseDetail> getByEDate(String edate);


    //添加数据
    @Insert("insert into expense_detail values(#{expDetailId},#{user.userId},#{expense.expId},#{bankId},#{dept.deptId},#{expDate},#{expPrice},#{expCredential},'','r')")
    void addEDetail(ExpenseDetail eDetail);

    //审批（未通过）
    @Update("update expense_detail set exp_feedback = #{efback},exp_detail_status = 'n' where exp_detail_id = #{edid}")
    void unpass(@Param("edid") int edid, @Param("efback") String efback);


    //审批通过
    @Update("update expense_detail set exp_feedback = #{efback},exp_detail_status = 'y' where exp_detail_id = #{edid}")
    void pass(@Param("edid") int edid, @Param("efback") String efback);


    //多条件查询
    @Select("select * from expense_detail,expense,dept ${ew.customSqlSegment} and expense_detail.exp_id = expense.exp_id and expense_detail.dept_id = dept.dept_id")
    @ResultMap("expenseDetailMap")
    List<ExpenseDetail> getByCondition(@Param(Constants.WRAPPER) Wrapper<ExpenseDetail> wrapper);


    //多条件分页查询
    @Select("select * from expense_detail,expense,dept ${ew.customSqlSegment} and expense_detail.exp_id = expense.exp_id and expense_detail.dept_id = dept.dept_id")
    @ResultMap("expenseDetailMap")
    Page<ExpenseDetail> getByConditionWithPage(Page<ExpenseDetail> page,@Param(Constants.WRAPPER) Wrapper<ExpenseDetail> wrapper);

    //分页查询
    @Select("select * from expense_detail,user,expense,dept where expense_detail.user_id = user.user_id and expense_detail.exp_id = expense.exp_id and expense_detail.dept_id = dept.dept_id and expense_detail.user_id = #{uid}")
    @ResultMap("expenseDetailMap")
    Page<ExpenseDetail> myListWithPage(Page<ExpenseDetail> page,@Param("uid") int uid);
}
